# Copyright 2017 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

GENDIR := $(abspath generated-src)
include Setting.mk
RVEC ?= 4100

default: $(GENDIR)/BottleRocketCore.v

.PHONY: $(GENDIR)/UCT_LDJ.v
$(GENDIR)/BottleRocketCore.v:
	rm -rf generated-src/*
	sbt "runMain bottlerocket.BottleRocketGenerator --target-dir generated-src --reset-vec ${RVEC}"

.PHONY: check-paths test clean

check-paths:
	sbt "runMain bottlerocket.BottleRocketPathChecker --target-dir path-check-output --reset-vec ${RVEC}"

test: $(GENDIR)/UCT_LDJ.v
	$(MAKE) -C test clean
	$(MAKE) -C test

clean:
	$(MAKE) -C test clean
	rm -rf generated-src


scp: $(GENDIR)/BottleRocketCore.v genProject
	scp -r $(LDJProject) $(USER)@$(targetHost):$(targetDir)
	rm -rf $(LDJProject)

createDir:
	rm -rf $(LDJProject)
	mkdir -p $(LDJProject)
	mkdir $(LDJProject)/src
	mkdir $(LDJProject)/src/verilog
	mkdir $(LDJProject)/src/soc
	mkdir $(LDJProject)/src/include
	mkdir $(LDJProject)/src/define

extraCoreFile:
	rm -rf temp
	mkdir temp
	cp third_party/rocket-chip/vsrc/* temp/
	cd temp/ ; ../script/Util_getmodule.pl $(GENDIR)/UCT_LDJ.v
	sed -i 's#//\s*\@.*$$##' temp/*.v   #Remove Comment
	./script/Util_extraModuleTree.pl temp/ $(LDJProject)/src/verilog $(LDJTopModule).v $(LDJProject)/src/include/include.v
	sed -i 's#$(LDJProject)#../../../ldj/rtl/$(LDJProject)#' $(LDJProject)/src/include/include.v
	rm -rf temp

genProject: createDir extraCoreFile


